<template>
	<view class="top-up-pay">
		<common-navbar :title="type== 1?'余额提现':'佣金提现'" />
		<view class="withdrawal-way">
			<view class="label">
				提现至:
			</view>
			<view @click="changeWithdrawalWay">
				<view class="withdrawal-way-right" v-if="currentWithdrawalWayType == 2||currentWithdrawalWayType == 3">
					<view class="wx-icon">
						<u-image
							src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-8acfaa58-aeb5-4502-8309-365d4c6cca4a/745138ee-19d0-455f-86f2-8c5dc9804cac.png"
							width="100%" height="100%" />
					</view>
					<view class="wx-name">
						{{currentWithdrawalWayType==2?'微信':currentWithdrawalWayType==3?'私下返款':''}}
					</view>
					<!-- 					<view class="account">
						（账号1234546）
					</view> -->
				</view>
				<view class="withdrawal-way-right" v-else>
					<view class="wx-name">
						小律懂法余额
					</view>
					<view class="account">
						（账号{{userInfo.phone}}）
					</view>
				</view>
			</view>
		</view>
		<view class="withdrawal-amount-container" v-if="maxAmount">
			<view class="withdrawal-amount">
				<view class="withdrawal-amount-label">
					可提现现金（元）
				</view>
				<view class="withdrawal-amount-value">
					{{maxAmount}}
				</view>
			</view>
			<view class="amount-input-wrapper">
				<view class="money-unit">
					￥
				</view>
				<view class="amount-input">
					<input v-model="withdrawalAmount" @input="$u.debounce(onInputHandle,500)" type="number"
						placeholder="请输入提现金额" placeholder-style="font-size:24rpx;color:#cccccc;"
						:maxlength="maxAmount.length" style="padding-right: 20rpx;">
				</view>
				<view class="all-withdrawal-btn" @click="withdrawalAmount = maxAmount">
					全部提取
				</view>
			</view>
		</view>
		<button class="withdrawal-btn" :disabled="disabled" :class="{'disabled':disabled}"
			@click="withdrawal">立即提现</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				withdrawalAmount: '',
				maxAmount: '',
				type: 1,
				currentWithdrawalWayType: 2,
				restUrl: '',
			}
		},
		computed: {
			disabled() {
				let reg = /^\d*\.?\d+$/;
				return !(Number(this.withdrawalAmount) > 0 && reg.test(this.withdrawalAmount));
			},
			withdrawalWay() {
				if (this.type == 1) {
					return [{
						name: "提现到微信",
					}]
				} else {
					return [{
						name: "提现到微信",
						value: 2,
					}, {
						name: "提现到余额",
						value: 1,
					}, {
						name: "私下返款",
						value: 3,
					}]
				}
			}
		},
		async onLoad(options) {
			this.type = options.type;
			const {
				balance,
				commission_balance
			} = (await this.$u.post(this.$restUrl.getAmount)).data;
			if (this.type == 1) {
				this.maxAmount = balance;
				this.restUrl = this.$restUrl.balanceWithdrawal;
			} else {
				this.maxAmount = commission_balance;
				this.restUrl = this.$restUrl.commissionWithdrawal;
			}
		},
		// watch: {
		// 	withdrawalAmount(val) {
		// 		if (parseFloat(val) > parseFloat(this.maxAmount)) {
		// 			this.withdrawalAmount = this.maxAmount;
		// 		}
		// 	}
		// },
		methods: {
			onInputHandle() {
				if (parseFloat(this.withdrawalAmount) > parseFloat(this.maxAmount)) {
					this.withdrawalAmount = this.maxAmount;
				}
			},
			changeWithdrawalWay() {
				if (this.type == 1) return
				uni.showActionSheet({
					itemList: this.withdrawalWay.map(item => item.name),
					success: async (res) => {
						this.currentWithdrawalWayType = this.withdrawalWay[res.tapIndex].value;
					},
				});
			},
			async withdrawal() {
				let reg = /^\d*\.?\d+$/;
				if (!reg.test(this.withdrawalAmount)) {
					return this.$showToast('请输入正确的金额')
				} else if (parseFloat(this.withdrawalAmount) > parseFloat(this.maxAmount)) {
					return this.$showToast('提现金额不可大于最大金额')
				}
				uni.showModal({
					title: '提现申请',
					content: `是否确认提交金额为${this.withdrawalAmount}元的提现申请`,
					success: async (res) => {
						if (res.confirm) {
							let options = {
								amount: this.withdrawalAmount,
								withdraw_type: this.currentWithdrawalWayType,
							}
							if (this.type == 1) delete options.withdraw_type;
							try {
								await this.$u.post(this.restUrl, options);
								uni.navigateBack({
									success: () => {
										const prepages = getCurrentPages()[getCurrentPages()
											.length - 1];
										console.log('prepages>>>>>>>>>>>>>>>>>>>>', prepages);
										prepages.$vm.getAmount();
										prepages.$vm.getWithdrawalList();
										this.$showToast('提交提现申请成功')
									}
								})
							} catch (e) {
								if (e.status_code == 405) {
									uni.showModal({
										title: '尚未上传收款二维码',
										content: '尚未上传收款二维码,请上传微信收款码之后再申请私下提现',
										success: () => {
											this.$u.route(
												'/pages_property/withdrawal_qrcode/withdrawalQRCode'
												)
										}
									})
								}
							}

						}
					}
				})


			}
		}
	}
</script>

<style scoped lang="scss">
	@import "./topUpPay.scss";
</style>
